﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PmSadis.Models.Abstract;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace PmSadis.Models
{
    /// <summary>
    /// Работа на элементе
    /// </summary>
    [Table("S_WORK")]
    public class Work : IRedactModel
    {
        /// <summary>
        /// Идентификатор
        /// </summary>
        [Column("WORK_ID")]
        public int WorkId { get; set; }

        /// <summary>
        /// Наименование
        /// </summary>
        [Column("NAME")]
        [Required]
        public string Name { get; set; }

        /// <summary>
        /// Код
        /// </summary>
        [Column("CODE")]
        public string Code { get; set; }

        /// <summary>
        /// Вид работ
        /// </summary>
        [Column("WORK_KIND")]
        public Models.Enums.WorkKind WorkKind { get; set; }

        /// <summary>
        /// Использовать детализацию при добавлении и редактировании
        /// </summary>
        [Column("USE_DETAIL")]
        public bool UseDetail { get; set; }

        [Column("UNIT_ID")]
        public int? UnitId { get; set; }
        public virtual Unit Unit { get; set; }

        /// <summary>
        /// Материалы работы
        /// </summary>
        public virtual ICollection<WorkMaterial> WorkMaterials { get; set; }

        /// <summary>
        /// Механизмы работы
        /// </summary>
        public virtual ICollection<WorkMechanism> WorkMechanisms { get; set; }

        /// <summary>
        /// Машинисты работы
        /// </summary>
        public virtual ICollection<WorkDriver> WorkDrivers { get; set; }

        /// <summary>
        /// Работники работы
        /// </summary>
        public virtual ICollection<WorkWorker> WorkWorkers { get; set; }

        /// <summary>
        /// Дата создания
        /// </summary>
        [Column("DATE_CREATE")]
        public DateTime DateCreate { get; set; }

        /// <summary>
        /// Дата редактирования
        /// </summary>
        [Column("DATE_UPDATE")]
        public DateTime DateUpdate { get; set; }

        [NotMapped]
        public double CostMaterialBase
        {
            get
            {
                return this.WorkMaterials.Sum(x => (double)x.Material.CostBase * x.Value);
            }
        }

        [NotMapped]
        public double CostMechanismBase
        {
            get
            {
                return this.WorkMechanisms.Sum(x => (double)x.Mechanism.CostBase * x.Value);
            }
        }
        [NotMapped]
        public double CostDriverBase
        {
            get
            {
                return this.WorkDrivers.Sum(x => (double)x.Driver.CostBase * x.Value);
            }
        }
        [NotMapped]
        public double CostWorkerBase
        {
            get
            {
                return this.WorkWorkers.Sum(x => (double)x.Worker.CostBase * x.Value);
            }
        }
        [NotMapped]
        public double CostBase
        {
            get
            {
                return this.CostMaterialBase + this.CostMechanismBase + this.CostDriverBase+this.CostWorkerBase;
            }
        }
    }
}
